$(function () {

    function doLoginFunc(url, data) {
        $.ajax({
                url: url,
                data: data,
                type: 'POST',
              beforeSend: function (request) {
                    var csrfToken = $("meta[name='_csrf']").attr("content");
                    var csrfHeader = $("meta[name='_csrf_header']").attr("content");
                    request.setRequestHeader(csrfHeader, csrfToken); // 添加  CSRF Token
                },
               success: function (data) {
                    if (data.status == 'error') {
                        alert(data.message ? data.message : "用户登录失败!");
                    } else if (data) {
                        //登录成功--模拟刷新页面
                        window.location.href = ctxPath+"success";
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseJSON ? jqXHR.responseJSON.message : "用户登录失败!");
                }
            }
        );
    }

    $('#loginBtn_web').bind('click', function () {
        var loginAcct = $('#j_loginAcct').val();
        var password = $('#j_password').val();
        var url = ctxPath + "login/web";
        var data = {'loginAcct': loginAcct, 'password': password};
        doLoginFunc(url, data);
    });

    $('#loginBtn_mobile').bind('click', function () {

        var mobile = $('#j_mobile').val();
        var code = $('#j_code').val();
        var url = ctxPath + "login/mobile";
        var data = {'mobile': mobile, 'code': code};

        doLoginFunc(url, data);
    });
});